﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Arthus
{
    public partial class dostavnicaStampanjeForma : Form
    {
        String pocetni, krajnji;
        //neophodno za štampanje
        StringFormat strFormat; //Used to format the grid rows.
        StringFormat strFormaRight;
        ArrayList arrColumnLefts = new ArrayList();//Used to save left coordinates of columns
        ArrayList arrColumnWidths = new ArrayList();//Used to save column widths
        int iCellHeight = 0; //Used to get/set the datagridview cell height
        int iTotalWidth = 0; //
        int iRow = 0;//Used as counter
        bool bFirstPage = false; //Used to check whether we are printing first page
        bool bNewPage = false;// Used to check whether we are printing a new page
        int iHeaderHeight = 0; //Used for the header height
        int magacin, preduzece;
        public dostavnicaStampanjeForma(String Pocetni, String Krajnji, int m, int p)
        {
            InitializeComponent();
            this.pocetni = Pocetni;
            this.krajnji = Krajnji;
            this.magacin = m;
            this.preduzece = p;
            this.WindowState = FormWindowState.Maximized;
            printDocumentStampanjeDostavnice.DefaultPageSettings.Landscape = true;
        }
        STV stvObjekat = null;
        private void dostavnicaStampanjeForma_Load(object sender, EventArgs e)
        {
            stvObjekat = new STV();
            stvObjekat.PopuniDataGridViewDostavnicaStampanje(dataGridViewStampanjeDostavnice, Convert.ToInt32(pocetni), Convert.ToInt32(krajnji), textBoxPreduzece,
                 textBoxDostavnicaInformacije, textBoxIzVPuMP, magacin, preduzece);
            foreach (DataGridViewRow red in dataGridViewStampanjeDostavnice.Rows)
            {
                foreach (DataGridViewCell celija in red.Cells)
                {
                    if (celija.Value != null)
                    {
                        if (celija.Value.ToString().Equals("Obračunao") || celija.Value.ToString().Equals("Odgovorno") || celija.Value.ToString().Equals("lice"))
                        {
                            celija.Style.Font = new Font("Arial", 10);
                        }
                    }
                }
            }
        }
        private void printDocumentStampanjeDostavnice_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                //ubacivanje u datatable

                //Set the left margin
                int iLeftMargin = e.MarginBounds.Left;
                //Set the top margin
                int iTopMargin = e.MarginBounds.Top;
                //Whether more pages have to print or not
                bool bMorePagesToPrint = false;
                int iTmpWidth = 0;

                //For the first page to print set the cell width and header height
                if (bFirstPage)
                {
                    //Draw Informacije o preduzecu
                    e.Graphics.DrawString(textBoxPreduzece.Text,
                        new Font("Arial", 12, FontStyle.Regular),
                        Brushes.Black, e.MarginBounds.Left,
                        e.MarginBounds.Top - e.Graphics.MeasureString(textBoxPreduzece.Text,
                        new Font("Arial", 12, FontStyle.Regular),
                        e.MarginBounds.Width).Height + 100);

                    //Draw Informacije o dostavnici
                    e.Graphics.DrawString(textBoxDostavnicaInformacije.Text,
                        new Font("Arial", 12, FontStyle.Regular),
                        Brushes.Black, e.MarginBounds.Left + 300,
                        e.MarginBounds.Top - e.Graphics.MeasureString(textBoxDostavnicaInformacije.Text,
                        new Font("Arial", 12, FontStyle.Regular),
                        e.MarginBounds.Width).Height + 50);

                    //Draw iz vp u mp
                    e.Graphics.DrawString(textBoxIzVPuMP.Text,
                        new Font("Arial", 12, FontStyle.Regular),
                        Brushes.Black, e.MarginBounds.Left + 700,
                        e.MarginBounds.Top - e.Graphics.MeasureString(textBoxIzVPuMP.Text,
                        new Font("Arial", 12, FontStyle.Regular),
                        e.MarginBounds.Width).Height + 50);

                    foreach (DataGridViewColumn GridCol in dataGridViewStampanjeDostavnice.Columns)
                    {
                        iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                            (double)iTotalWidth * (double)iTotalWidth *
                            ((double)e.MarginBounds.Width / (double)iTotalWidth))));

                        iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                            GridCol.InheritedStyle.Font, iTmpWidth).Height) + 10;

                        // Save width and height of headers
                        arrColumnLefts.Add(iLeftMargin);
                        arrColumnWidths.Add(iTmpWidth);
                        iLeftMargin += iTmpWidth;
                    }
                }
                //Loop till all the grid rows not get printed

                while (iRow <= dataGridViewStampanjeDostavnice.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dataGridViewStampanjeDostavnice.Rows[iRow];
                    //Set the cell height
                    iCellHeight = GridRow.Height + 5;
                    int iCount = 0;
                    //Check whether the current page settings allows more rows to print
                    if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        bNewPage = true;
                        bFirstPage = false;
                        bMorePagesToPrint = true;
                        break;
                    }
                    else
                    {
                        if (bNewPage)
                        {
                            //Draw Columns                 
                            iTopMargin = e.MarginBounds.Top;
                            foreach (DataGridViewColumn GridCol in dataGridViewStampanjeDostavnice.Columns)
                            {
                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                    new Rectangle((int)arrColumnLefts[iCount], iTopMargin + 100,
                                    (int)arrColumnWidths[iCount], iHeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                    new Rectangle((int)arrColumnLefts[iCount], iTopMargin + 100,
                                    (int)arrColumnWidths[iCount], iHeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText,
                                    GridCol.InheritedStyle.Font,
                                    new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                    new RectangleF((int)arrColumnLefts[iCount], iTopMargin + 100,
                                    (int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
                                iCount++;
                            }
                            bNewPage = false;
                            iTopMargin += iHeaderHeight;
                        }
                        iCount = 0;
                        int indexReda = 0;
                        //Draw Columns Contents                
                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (Cel.Value != null && Cel.Value.ToString() != "")
                            {
                                if (Cel.Value.ToString().Equals("UKUPNO:") || Cel.Value.ToString()== "UKUPNO:")
                                {
                                    indexReda = Cel.RowIndex;
                                }
                                decimal n;
                                bool isNumber = decimal.TryParse(Cel.Value.ToString(), out n);
                                if (isNumber)
                                {
                                    e.Graphics.DrawString(Cel.Value.ToString(),
                                        Cel.InheritedStyle.Font,
                                        new SolidBrush(Cel.InheritedStyle.ForeColor),
                                        new RectangleF((int)arrColumnLefts[iCount],
                                        (float)iTopMargin + 100,
                                        (int)arrColumnWidths[iCount], (float)iCellHeight),
                                        strFormaRight);
                                }
                                else
                                {
                                    e.Graphics.DrawString(Cel.Value.ToString(),
                                            Cel.InheritedStyle.Font,
                                            new SolidBrush(Cel.InheritedStyle.ForeColor),
                                            new RectangleF((int)arrColumnLefts[iCount],
                                            (float)iTopMargin + 100,
                                            (int)arrColumnWidths[iCount], (float)iCellHeight),
                                            strFormat);
                                }
                                //Drawing Cells Borders
                                if (!Cel.Value.ToString().Equals("Obračunao") && !Cel.Value.ToString().Equals("Odgovorno") && !Cel.Value.ToString().Equals("lice"))
                                {
                                    e.Graphics.DrawRectangle(Pens.Black,
                                    new Rectangle((int)arrColumnLefts[iCount], iTopMargin + 100,
                                    (int)arrColumnWidths[iCount], iCellHeight));
                                }
                                else {
                                    e.Graphics.DrawLine(Pens.Black, new Point((int)arrColumnLefts[iCount], iTopMargin + 100 + iCellHeight + iCellHeight),
                                        new Point((int)arrColumnLefts[iCount] + (int)arrColumnWidths[iCount], iTopMargin + 100 + iCellHeight + iCellHeight));
                                }
                            }
                            iCount++;
                        }
                    }
                    iRow++;
                    iTopMargin += iCellHeight;
                }
                //If more lines exist, print another page.
                if (bMorePagesToPrint)
                    e.HasMorePages = true;
                else
                    e.HasMorePages = false;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK,
                   MessageBoxIcon.Error);
            }
        }//stampanje

        private void printDocumentStampanjeDostavnice_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            try
            {
                strFormat = new StringFormat();
                strFormat.Alignment = StringAlignment.Near;
                strFormat.LineAlignment = StringAlignment.Center;
                strFormat.Trimming = StringTrimming.EllipsisCharacter;

                strFormaRight = new StringFormat();
                strFormaRight.Alignment = StringAlignment.Far;
                strFormaRight.LineAlignment = StringAlignment.Center;
                strFormaRight.Trimming = StringTrimming.EllipsisCharacter;

                arrColumnLefts.Clear();
                arrColumnWidths.Clear();
                iCellHeight = 0;
                iRow = 0;
                bFirstPage = true;
                bNewPage = true;

                // Calculating Total Widths
                iTotalWidth = 0;
                foreach (DataGridViewColumn dgvGridCol in dataGridViewStampanjeDostavnice.Columns)
                {
                    iTotalWidth += dgvGridCol.Width;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonStampajPregled_Click(object sender, EventArgs e)
        {
            //Open the print preview dialog
            PrintPreviewDialog objPPdialog = new PrintPreviewDialog();
            objPPdialog.Document = printDocumentStampanjeDostavnice;
            PrintDialog objPrintDialog = new PrintDialog();
            ((Form)objPPdialog).WindowState = FormWindowState.Maximized;
            objPPdialog.ShowDialog();
        }

        private void buttonStampaj_Click(object sender, EventArgs e)
        {
            PrintDialog printDialogObjekat = new PrintDialog();
            printDialogObjekat.Document = printDocumentStampanjeDostavnice;
            printDialogObjekat.ShowDialog();
        }


    }
}
